package com.example.teachingevaluationsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.teachingevaluationsystem.entity.CourseOrder;
import com.example.teachingevaluationsystem.entity.EvaluationStatus;
import com.example.teachingevaluationsystem.entity.StudentEvaluate;
import com.example.teachingevaluationsystem.entity.TeacherEvaluation;
import com.example.teachingevaluationsystem.mapper.CourseOrderMapper;
import com.example.teachingevaluationsystem.service.CourseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingevaluationsystem.service.EvaluationStatusService;
import com.example.teachingevaluationsystem.service.StudentEvaluateService;
import com.example.teachingevaluationsystem.service.TeacherEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程订阅 服务实现类
 *
 * @author lp
 * @since 2022-03-28
 */
@Service
public class CourseOrderServiceImpl extends ServiceImpl<CourseOrderMapper, CourseOrder>
    implements CourseOrderService {

  @Autowired private StudentEvaluateService studentEvaluateService;
  @Autowired private TeacherEvaluationService teacherEvaluationService;
  @Autowired private EvaluationStatusService evaluationStatusService;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void deleteStatus(Long id, Long planId, Long courseId) {
    studentEvaluateService.remove(
        new LambdaQueryWrapper<StudentEvaluate>()
            .eq(StudentEvaluate::getStudentId, id)
            .eq(StudentEvaluate::getPlanId, planId)
            .eq(StudentEvaluate::getCourseId, courseId));
    teacherEvaluationService.remove(
        new LambdaQueryWrapper<TeacherEvaluation>()
            .eq(TeacherEvaluation::getCourseId, courseId)
            .eq(TeacherEvaluation::getPlanId, planId)
            .eq(TeacherEvaluation::getTeacherId, id));
    evaluationStatusService.remove(
        new LambdaQueryWrapper<EvaluationStatus>()
            .eq(EvaluationStatus::getCourseId, courseId)
            .eq(EvaluationStatus::getPlanId, planId)
            .eq(EvaluationStatus::getUserId, id));
  }
}
